//
//  ActivityListViewController.m
//  豆瓣YHY
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityListViewCell.h"
#import "Activitylist.h"
#import "DataBaseSingleton.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
@interface ActivityListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)MBProgressHUD *hub;
@property (nonatomic,copy)NSString *filePath;
@end

@implementation ActivityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allArray = [NSMutableArray array];
//    [AppTools getLocalDataWithURL:@"activitylist.txt" andBlock:^(NSDictionary *dic) {
//        
//        NSMutableArray *array = [dic objectForKey:@"events"];
//        
//     //   NSLog(@"array = %@",array);
//        
//        for (NSDictionary *dic in array) {
//            
//            Activitylist *activity = [[Activitylist alloc] init];
//       //     activity.begin_time = [dic objectForKey:@"time"];
//            [activity setValuesForKeysWithDictionary:dic];//
//            [self.allArray addObject:activity];
//            [activity release];
//
    
    
    
    
    
    //加菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载";
    
   //打开数据库
    [[DataBaseSingleton shareDataBase]openDB];
     //创建表
        [[DataBaseSingleton shareDataBase]creatTable];
    
   //self.allArray = [[DataBaseSingleton shareDataBase]selectActivity];
    [[DataBaseSingleton shareDataBase]selectActivity];
      self.allArray = [DataBaseSingleton shareDataBase].array;
    NSLog(@"rrrrr%@",self.allArray);
//    //查询所有学生
//    [[DataBaseSingleton shareDataBase]selectActivity];
    
    
    
//    self.allArray =  [[DataBaseSingleton shareDataBase]selectActivity];
    [AppTools getLocalDataWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" andBlock:^(id dic) {
       
        NSMutableDictionary *dica = dic;
       // NSLog(@"%@",dica);
        NSMutableArray *array = [dica objectForKey:@"events"];
      
          //  NSLog(@"array = %@",array);
        
        for (NSMutableDictionary *dicb in array) {
                    
                    Activitylist *activity = [[Activitylist alloc] init];//属性
                    //activity.begin_time = [dica objectForKey:@"time"];
                    [activity setValuesForKeysWithDictionary:dicb];//属性里传值
            
            
            //+插入对象+值
            [[DataBaseSingleton shareDataBase]insertStudent:activity];
            
                    [AppTools getLocalDataWithimage:activity.image andBlock:^(UIImage *image) {
                        
                        
                        activity.image_hlarge = image;
                        
                    }];
                    
                    [self.allArray addObject:activity];//每
                    [activity release];
                   
                }
        
      //  [self.mydic setObject:self.allArray forKey:@"time"];
       
     //   NSLog(@"zzzzz%@",self.allArray);
        [self.activityTableView reloadData];
                }];
   
    

    
    
    


    
    
    
    self.navigationItem.title = @"活动";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.activityTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style: UITableViewStylePlain];
    self.activityTableView.delegate =self;
    self.activityTableView.dataSource =self;
    [self.view addSubview:self.activityTableView];
    [_activityTableView release];
//
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 186;//
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndefider = @"cell";
    ActivityListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndefider];
    if (cell == nil) {
        cell = [[ActivityListViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefider];
    }
    
    
    Activitylist *activity = [self.allArray objectAtIndex:indexPath.row];
    
    cell.activity = activity;
    
    return cell;
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allArray.count;
    
    
}

 //点击cell触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetaViewController *activityDetaViewController = [[ActivityDetaViewController alloc]init];
//    Activitylist *activitylist = [self.allArray objectAtIndex:indexPath.row];//
//    activityDetaViewController.actilist = activitylist;
    
    
   Activitylist *activity =[self.allArray objectAtIndex:indexPath.row];
   activityDetaViewController.actilist =activity;
    
    
    
    
    
    
    
    
    
    
    
    //到下一个页
    [self.navigationController pushViewController:activityDetaViewController animated:YES];
    
    
    
   
    
 
 }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
